package org.jeecg.modules.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.modules.entity.DbContract;
import org.jeecg.modules.entity.DbEmpContract;
import org.jeecg.modules.entity.DbEmployeeData;
import org.jeecg.modules.entity.DbFile;
import org.jeecg.modules.mapper.DbEmpContractMapper;
import org.jeecg.modules.service.IDbEmpContractService;
import org.jeecg.modules.service.IDbFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: db_emp_contract
 * @Author: jeecg-boot
 * @Date:   2024-07-08
 * @Version: V1.0
 */
@Service
public class DbEmpContractServiceImpl extends ServiceImpl<DbEmpContractMapper, DbEmpContract> implements IDbEmpContractService {

    @Autowired
    private IDbFileService dbFileService;
    /**
     *   添加
     *
     * @param dbEmpContract
     * @return
     */
    @Transactional
    public void add(DbEmpContract dbEmpContract) {
        updateFileGroupId(dbEmpContract);
    }


    //    添加附件id
    private void updateFileGroupId(DbEmpContract dbEmpContract) {
        if (dbEmpContract.getFileIds() == null || dbEmpContract.getFileIds().isEmpty()) {
            this.save(dbEmpContract);
        } else {
            dbEmpContract.setFileGroupId(null);
            this.save(dbEmpContract);
            UpdateWrapper<DbEmpContract> dbEmpContractUpdateWrapper = new UpdateWrapper<>();
            String fileGroupId = "db_empContract" + dbEmpContract.getId();
            dbEmpContractUpdateWrapper.set("file_group_id", fileGroupId).eq("id", dbEmpContract.getId());
            this.update(dbEmpContract, dbEmpContractUpdateWrapper);
            UpdateWrapper<DbFile> updateWrapper = new UpdateWrapper<>();
            DbFile dbFile = new DbFile();
            updateWrapper.set("group_id", fileGroupId).in("id", dbEmpContract.getFileIds());
            dbFileService.update(dbFile, updateWrapper);
        }
    }
}
